package com.shuai.baibi.interceptor;

import com.shuai.baibi.common.ErrorCode;
import com.shuai.baibi.constant.UserConstant;
import com.shuai.baibi.exception.BusinessException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 用户统一登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(UserConstant.LOGIN_STATE_KEY) == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "用户未登录！");
        }
        return true;
    }
}
